package com.youwu.weather.job;

import com.youwu.weather.service.CityDataService;
import com.youwu.weather.service.WeatherDataService;
import com.youwu.weather.vo.City;
import com.youwu.weather.vo.WeatherType;
import org.apache.commons.collections4.CollectionUtils;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;

import java.util.List;

public class WeatherDataSyncJob extends QuartzJobBean {

    private static Logger logger = LoggerFactory.getLogger(WeatherDataSyncJob.class);

    @Autowired
    private CityDataService cityDataService;
    @Autowired
    private WeatherDataService weatherDataService;

    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        logger.info("quartz has done");
        // 获取城市数据
        List<City> cityList = cityDataService.listCity();
        // 遍历城市
        if (CollectionUtils.isNotEmpty(cityList)) {
            logger.info("城市数据获取成功！");
            for (int i=0; i < cityList.size(); i++) {
                System.out.println(cityList.get(i).getAdminDistrictCn() + "," + cityList.get(i).getCityCn());
            }

            // 由于api接口调用限制，这边只获取一条数据用来测试
            weatherDataService.syncDataByCity(WeatherType.NOW, "思明区,厦门");
        } else {
            logger.error("城市数据获取失败！");
        }
    }
}
